package com.lingkang.dbtodoc.utils;

/**
 * @author 绫小路
 * @date 2021/2/1 19:24
 * @description
 */
public class DbSqlUtils {

  private static final String MYSQL = "mysql";

  /**
   * 初始化SQL
   */
  public static String init(String type) {
    if (MYSQL.equals(type)) {
      return "select 1 as success";
    }
    return null;
  }

  public static String getDataBaseName(String type) {
    if (MYSQL.equals(type)) {
      return "select DATABASE() as dataBaseName";
    }
    return null;
  }

  public static String getVersion(String type) {
    if (MYSQL.equals(type)) {
      return "select version() as version;";
    }
    return null;
  }

  public static String get(String type) {
    if (MYSQL.equals(type)) {
      return "SELECT TABLE_SCHEMA AS dataBaseName, TABLE_NAME AS tableName, `ENGINE` AS `engine`, VERSION AS version, TABLE_ROWS AS `rows` , AVG_ROW_LENGTH AS avgRowSize, DATA_LENGTH AS size, AUTO_INCREMENT AS autoIncrement, CREATE_TIME AS createTime, TABLE_COLLATION AS tableCollation , CREATE_TIME AS tableComment, TABLE_COMMENT AS tableComment FROM information_schema.TABLES WHERE TABLE_SCHEMA = ( SELECT DATABASE() )";
    }
    return null;
  }

  public static String updateTableComment(String type, String table, String comment) {
    if (MYSQL.equals(type)) {
      return String.format("alter table %s comment '%s'", table, comment);
    }
    return null;
  }

  /**
   * 获取表的列信息
   */
  public static String getColumn(String type, String tableName) {
    if (MYSQL.equals(type)) {
      return String.format(
          "SELECT TABLE_SCHEMA AS dataBaseName, TABLE_NAME AS tableName, COLUMN_NAME AS columnName, COLUMN_TYPE AS columnType, COLUMN_COMMENT AS columnComment,ORDINAL_POSITION as ordinalPosition,IS_NULLABLE as isNullable,CHARACTER_SET_NAME as `character` FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = ( SELECT DATABASE() ) AND TABLE_NAME = '%s'",
          tableName);
    }
    return null;
  }

  public static String updateColumnComment(String type, String tableName, String columnName, String comment) {
    if (MYSQL.equals(type)) {
      return String.format("ALTER TABLE %s MODIFY COLUMN %s INT COMMENT '%s';", tableName, columnName, comment);
    }
    return null;
  }


}
